<?php
import( 'de.ceus-media.framework.neon.DefinitionView' );
import( 'classes.models.RightObject' );
import( 'classes.models.RightAction' );
import( 'classes.models.RightObjectAction' );
/**
 *	Views of Right Objects.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionView
 *	@uses			RoleObject
 *	@uses			RightAction
 *	@uses			RightObjectAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		0.6
 */
/**
 *	Views of Right Objects.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionView
 *	@uses			RoleObject
 *	@uses			RightAction
 *	@uses			RightObjectAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		0.6
 *	@todo			Language Support of Messages
 *	@todo			Code Documentation
 */
class RightObjectViews extends CMF_Krypton_DefinitionView
{
	/**
	 *	Builds Content Views.
	 *	@access		public
	 *	@return		void
	 */
	public function buildContent()
	{
		$request	= $this->ref->get( "request" );
		$auth	= $this->ref->get( 'auth' );

		$content	= "";
		if( $rightobject_id	= $request->get( 'rightobject_id' ) )
		{
			if( $auth->hasRight( 'right_object', 'edit' ) )
				$content	.= $this->buildFieldEdit();
			if( $auth->hasRight( 'right_object', 'edit' ) )
				$content	.= $this->buildFieldActions();
		}
		else
		{
			if( $auth->hasRight( 'right_object', 'list' ) )
				$content	.= $this->buildFieldList();
			if( $auth->hasRight( 'right_object', 'add' ) )
				$content	.= $this->buildFieldAdd();
		}
		return $content;
		
	}

	/**
	 *	Builds Control View from Wiki File.
	 *	@access		public
	 *	@return		string
	 */
	public function buildControl()
	{
		return $this->loadContent( "right_object_control.wiki" );
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildFieldAdd()
	{
		$config	= $this->ref->get( 'config' );
		$request	= $this->ref->get( 'request' );
		$words	= $this->words['right_object']['add'];

		$ui	= $this->buildForm( 'right_object', 'addObject', 'right_object', 'add' );

//		$input_object		= $this->html->Input( 'add_object',		$request->get( 'add_object' ), 'in' );
//		$input_description	= $this->html->TextArea( 'add_description',	$request->get( 'add_description' ), 'xs' );
		$button_add		= $this->html->Button( 'addObject',		$words['button_add'] );

//		$ui['label_object']		= $this->html->Label( 'add_object',		$words['add_object'] );
//		$ui['label_description']	= $this->html->Label( 'add_description',	$words['add_description'] );

//		$ui['field_object']		= $this->html->Field( 'add_object',		$input_object );
//		$ui['field_description']	= $this->html->Field( 'edit_description',	$input_description );
		$ui['field_button']		= $this->html->Field( '', $button_add );

		$ui['form']		= $this->html->Form( 'addObject', "?link=".$request->get( 'link' ) );
		$ui['colgroup']		= $this->html->ColumnGroup( "30%", "70%" );
		$ui['caption']		= $this->html->TableCaption( $words['caption_add'] );
		$ui['heading']		= $words['heading_add'];
		
		return $this->loadTemplate( "right.object.add", $ui );
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildFieldEdit()
	{
		$config	= $this->ref->get( 'config' );
		$request	= $this->ref->get( 'request' );
		$words	= $this->words['right_object']['edit'];

		if( $rightobject_id = $request->get( 'rightobject_id' ) )
		{
			$opt_action		= array(
				"add"	=> $words['action_add'],
				"edit"	=> $words['action_edit'],
				);

			$object	= new RightObject( $rightobject_id );
			$data	= $object->getData( TRUE );

			$ui	= $this->buildForm( 'right_object', 'editObject', 'right_object', 'edit', $data );

//			$input_object		= $this->html->Input( 'edit_object',		$data['title'], 'in' );
//			$input_description	= $this->html->TextArea( 'edit_description',	$data['description'], 'xs' );
			$button_edit		= $this->html->Button( 'editObject',		$words['button_edit'] );
			$button_remove	= $this->html->Button( 'removeObject',	$words['button_remove'], 'buthot', $words['button_remove_confirm'] );

//			$ui['label_object']		= $this->html->Label( 'edit_object',		$words['edit_object'] );
//			$ui['label_description']	= $this->html->Label( 'edit_description',	$words['edit_description'] );

//			$ui['field_object']		= $this->html->Field( 'edit_object',		$input_object );
//			$ui['field_description']	= $this->html->Field( 'edit_description',	$input_description );
			$ui['field_buttons']		= $this->html->Field( '', $button_edit.$button_remove );

			$ui['form']		= $this->html->Form( 'editObject', "?link=".$request->get( 'link' )."&rightobject_id=".$request->get( 'rightobject_id' ) );
			$ui['colgroup']		= $this->html->ColumnGroup( "30%", "70%" );
			$ui['caption']		= $this->html->TableCaption( $words['caption_edit'] );
			$ui['heading']		= $words['heading_edit'];

			return $this->loadTemplate( "right.object.edit", $ui );
		}
		else
			$this->messenger->noteError( $this->words['msg']['error1'] );
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildFieldList()
	{
		$config	= $this->ref->get( 'config' );
		$request	= $this->ref->get( 'request' );
		$auth	= $this->ref->get( 'auth' );
		$words	= $this->words['right_object']['list'];

		$editor	= $auth->hasRight( 'right_object', 'edit' );

		$list		= array();
		$object	= new RightObject();
		$objects	= $object->getAllData();
		if( count( $objects ) )
		{
			$i = 0;
			foreach( $objects as $object )
			{
				$label_desc	= $this->html->Label( '', $object['description'] );
				$style	= ++$i % 2 ? 'list1' : 'list2';
				if( $editor )
				{
					$link			= $this->html->Link( "?link=".$request->get( 'link' )."&rightobject_id=".$object['rightobject_id'], $object['title'] );
					$label_link	= $this->html->Label( '', $link );
				}
				else
					$label_link	= $this->html->Label( '', $object['title'] );
				$list[]	= "<tr class='".$style."'>".$label_link.$label_desc."</tr>";
			}
			$ui['objects']	= implode( "", $list);
		}
		else
			$ui['objects']	= "<tr>".$this->html->Label( '', $words['no_entries'] )."</tr>";

		$heads	= array(
			$words['head_object'],
			$words['head_description'],
		);
		$ui['heads']		= $this->html->TableHeads( $heads );
		$ui['colgroup']		= $this->html->ColumnGroup( "30%", "70%" );
		$ui['caption']		= $this->html->TableCaption( $words['caption_list'] );
		$ui['heading']		= $words['heading_list'];

		return $this->loadTemplate( "right.object.list", $ui );
	}
	
	private function buildFieldActions()
	{
		$config	= $this->ref->get( 'config' );
		$request	= $this->ref->get( 'request' );
		$auth	= $this->ref->get( 'auth' );
		$words	= $this->words['right_object']['actions'];
	
		if( $rightobject_id = $request->get( 'rightobject_id' ) )
		{
			
			$relation	= new RightObjectAction();

			$action	= new RightAction();
			$actions	= $action->getAllData();

			$opt_actions		= array();
			$opt_actions_has	= array();
			foreach( $actions as $action )
			{
				if( $relation->isObjectActionByID( $rightobject_id, $action['rightaction_id'] ) )
					$opt_actions_has[$action['rightaction_id']]	= $action['title'];
				else
					$opt_actions[$action['rightaction_id']]	= $action['title'];
			}

			$sources	= array(
				'opt_actions'		=> $opt_actions,
				'opt_actions_has'	=> $opt_actions_has,
				);
			$inputs	= $this->buildInputs( 'right_object', 'editObjectActions', 'right_object', 'actions', array(), $sources );
			$ui		= $this->buildFields( 'right_object', 'editObjectActions', 'right_object', 'actions', $inputs );

//			$input_actions		= $this->html->Select( 'actions[]', $opt_actions, 'sm' );
//			$input_actions_has	= $this->html->Select( 'actions_has[]', $opt_actions_has, 'sm' );
			$button_add		= $this->html->Button( 'addActions', $words['button_add'] );
			$button_remove	= $this->html->Button( 'removeActions', $words['button_remove'], 'buthot', $words['button_remove_confirm'] );

//			$ui['field_actions']		= $this->html->Field( '', $input_actions );
//			$ui['field_actions_has']	= $this->html->Field( '', $input_actions_has );
			$ui['field_button_add']		= $this->html->Field( '', $button_add );
			$ui['field_button_remove']	= $this->html->Field( '', $button_remove );

			$ui['form']		= $this->html->Form( 'editObjectActions', "?link=".$request->get( 'link' )."&rightobject_id=".$rightobject_id );
			$ui['colgroup']		= $this->html->ColumnGroup( "20%", "40%", "40%" );
			$ui['caption']		= $this->html->TableCaption( $words['caption_actions'] );
			$ui['heading']		= $words['heading_actions'];

			return $this->loadTemplate( "right.object.actions", $ui );
		}
		else
			$this->messenger->noteError( $this->words['msg']['error1'] );
	}
}
?>
